package com.pactera.chatRoom.util;

import javax.annotation.Resource;

import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.ArrayList;
import java.util.List;

public class RedisUtil {
	
	@Resource
	private static StringRedisTemplate strTemplate;

	public static void testRedis() {
		strTemplate.opsForValue().set("name", "Kevin");
		String name = strTemplate.opsForValue().get("name");
		System.out.println(name);
	}
	
	//登录成功后把人员信息记录在redis中
	public static Long setPersonOnlineStatus(String username) {
		
		Long num = strTemplate.opsForList().leftPush("OnlineList", username);
		
		return num;
	}
	
	//查询在线人员
	public static List getOnlineStatus() {

		List allOnlinePerson = new ArrayList();
		List onlineList = strTemplate.opsForList().range("OnlineList",0,-1);

		for (int i=0;i<onlineList.size();i++){
			allOnlinePerson.add(strTemplate.opsForList().index("list", i));
		}
		return allOnlinePerson;
	}
	
	//人员下线时从redis中删除该人员在线信息
	public static Long remove(String username) {
        Long remove = strTemplate.opsForList().remove("OnlineList", 0, username);
        return remove;
    }
	
	
	//查询当前在线人数
	public static Long size() {
		Long size = strTemplate.opsForList().size("OnlineList");
        return size;
    }

    //以用户姓名作为主键，点赞则加一
	public void increment(String username) {
		strTemplate.opsForValue().increment(username);
	}

	//查询点赞次数
    public String getApproveNum(String username){
        return strTemplate.opsForValue().get(username);
    }

}
